﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.NetworkInformation;
using System.Xml;
using mediaFunction;

namespace homeMediaSetup
{

    class htpcServerControl 
    {
        //load PowrProf.dll so standby function can be called
        [DllImport("PowrProf.dll")]
        public static extern bool SetSuspendState(bool Hibernate, bool ForceCritical, bool DisableWakeEvent);
        static void Main(string[] args)
        {
            //initial declaration and value
            bool clientOff = true;

            //declare class so local methods may be used
            xmlHandler myXmlHandler = new xmlHandler();
            sabnzbdAccess mySabAccess = new sabnzbdAccess("192.168.1.64", "8080", "d9244b753a5bc874e91bc59ed6430baf");


            //declarations and functions for pinging client PC
            int timeout = 300;
            IPAddress rcvHtpcAddr = IPAddress.Parse("192.168.1.139");
            Ping pingSender = new Ping();
            PingReply reply = pingSender.Send(rcvHtpcAddr, timeout);
            clientOff = clientOff && reply.Status != IPStatus.Success;

            //declarations and functions for pinging laptop0            
            IPAddress homeLaptop0 = IPAddress.Parse("192.168.1.101");
            reply = pingSender.Send(homeLaptop0, timeout);
            clientOff = clientOff && reply.Status != IPStatus.Success;

            //declarations
            XmlTextReader sabnzbdStatus;
            bool valueFound;
            bool serverQueueEmpty= false;
            string nodeValue = null;

            //sabnzbdStatus = new XmlTextReader(mySabAccess.xmlDump());
            sabnzbdStatus = mySabAccess.xmlDump();

            sabnzbdStatus.WhitespaceHandling = WhitespaceHandling.Significant;

            //call function to check for server empty
            valueFound = myXmlHandler.findValue(sabnzbdStatus, "noofslots", ref nodeValue);
                        
            //check if server is empty            
            if (valueFound)
            {
                if (nodeValue == "0")
                {
                    serverQueueEmpty = true;
                }
            }
            

            if (clientOff && serverQueueEmpty)
            {
                //if the client device cannot be found, it is safe to shut-down the server
                //this server task will not be entered until the server itself is considered idle,
                //so no need to check for idle here.
                Console.WriteLine("Client PC1 is off");
                Console.WriteLine("Server Queue is empty");


                /*
                 * clear sabnzbd history
                 */
                mySabAccess.sabnzbdClrHistory();

                //suspend computer with 
                //1. standby mode
                //2. force shtudown, does not broadcast suspend message to other processes
                //3. allow scheduled tasks to wake up standby computer
                SetSuspendState(false, true, false);                
            }
            else{
                Console.WriteLine("Not all idle conditions met, wait for next checkpoint");
            }
                        
        }
    }
}
